﻿using System;
using System.Data;
using System.Web.UI;
using MyWeb.Business;
using MyWeb.Data;

namespace MyWeb.Admin.Usercontrols
{
    public partial class ucSupportAddUpdate : UserControl
    {
        private readonly SupportService _supportService = new SupportService();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int insert = Int32.Parse(Request.QueryString["in"]);
                if (insert == 1)
                {
                    RefreshControl();
                }
                else
                {
                    int id = Int32.Parse(Request.QueryString["id"]);
                    DataTable dt = _supportService.Support_GetById(id);
                    txtName.Text = dt.Rows[0]["name"].ToString();
                    txtTel.Text = dt.Rows[0]["tel"].ToString();
                    txtNick.Text = dt.Rows[0]["nick"].ToString();
                    txtOrder.Text = dt.Rows[0]["order"].ToString();
                    ddlType.SelectedValue = dt.Rows[0]["type"].ToString();
                    if (Int32.Parse(dt.Rows[0]["status"].ToString()) == 0)
                    {
                        ckCongbo.Checked = true;
                    }
                    else
                    {
                        ckCongbo.Checked = false;
                    }
                }
            }
        }

        private void RefreshControl()
        {
            txtName.Text = string.Empty;
            txtNick.Text = string.Empty;
            txtOrder.Text = string.Empty;
            txtTel.Text = string.Empty;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            int status = ckCongbo.Checked ? 0 : 1;
            var support = new SupportInfo
                              {
                                  Name = txtName.Text,
                                  Tel = txtTel.Text,
                                  Type = Int32.Parse(ddlType.SelectedValue),
                                  Nick = txtNick.Text,
                                  Order = Int32.Parse(txtOrder.Text),
                                  Status = status
                              };
            if (Int32.Parse(Request.QueryString["in"]) == 1)
            {
                _supportService.Support_Insert(support);
            }
            else
            {
                support.Id = Int32.Parse(Request.QueryString["id"]);
                _supportService.Support_Update(support);
            }
            Response.Redirect("~/Admin/View.aspx?action=ucSupportView");
        }

        protected void btnSaveAndNew_Click(object sender, EventArgs e)
        {
            int status = ckCongbo.Checked ? 0 : 1;
            var support = new SupportInfo
                              {
                                  Name = txtName.Text,
                                  Tel = txtTel.Text,
                                  Type = Int32.Parse(ddlType.SelectedValue),
                                  Nick = txtNick.Text,
                                  Order = Int32.Parse(txtOrder.Text),
                                  Status = status
                              };
            if (Int32.Parse(Request.QueryString["in"]) == 1)
            {
                _supportService.Support_Insert(support);
            }
            else
            {
                support.Id = Int32.Parse(Request.QueryString["id"]);
                _supportService.Support_Update(support);
            }
            RefreshControl();
        }

        protected void btnCancelAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Admin/View.aspx?action=ucSupportView");
        }
    }
}